$(document).ready(function () {
    function strip(input) {
        return input.replace(/^\s+|\s+$/, '');
    }

    $("#match").click(function () {
        var match_type = $('#control input:radio[name="inlineRadioOptions"]:checked').val();

        var city = strip($('#city').val());
        var salary = strip($('#salary').val());
        var job_name = strip($('#job_name').val());
        var introduce = strip($('#introduce').val());
        var work_time = strip($('#work_time').val());
        var education = strip($('#education').val());
        var industry = strip($('#industry').val());

        if (!job_name) {
            return alert('请您输入岗位名称');
        }

        if (!salary) {
            return alert('请您输入目标薪水');
        }

        if (!work_time) {
            return alert('请您输入工作经验');
        }

        if (salary && !/^\d+$/.test(salary) && !/\d+\-\d+/.test(salary)) {
            return alert('请您输入正确的薪资格式:6000或者6000-8000');
        }

        if (work_time && !/^\d+$/.test(work_time)) {
            return alert('[工作经验]请您输入正确的格式');
        }

        if (!education) {
            return alert('请您输入学历');
        }

        if (!industry) {
            return alert('请您输入目标行业');
        }

        var that = this;

        $(this).text('正在匹配...');

        var dict = {
            '51Job': '51Job',
            'dajie': 'dajie',
            'zhilian': '智联'
        }

        $('.posts_container').addClass('hidden');

        $.ajax({
            url: '/match_resume',
            dataType: 'json',
            data: {
                city: city,
                salary: salary,
                job_name: job_name,
                industry: industry,
                education: education,
                introduce: introduce,
                work_time: work_time,
                match_type: match_type
            },
            success: function (resp) {
                if (resp.success) {
                    var table_doc = "";

                    for (var i = 0; i < resp.data.length; i++) {
                        var item = resp.data[i];

                        table_doc += '<tr>\n' +
                            '            <th scope = "row">' + (i + 1) + '</th>\n' +
                            '            <td>' + item.company.name + '</td>\n' +
                            '            <td>' + item.company.industry + '</td>\n' +
                            '            <td>' + item.job_name + '</td>\n' +
                            '            <td>' + (item.salary.join('-')) + '元' + '</td>\n' +
                            '            <td>' + item.city + '</td>\n' +
                            '            <td>' + (item.work_time.length ? item.work_time.join('-') + '年' : '不限') + '</td>\n' +
                            '            <td>' + (item.education ? item.education : "不限") + '</td>\n' +
                            '            <td><a href = "' + (item.ref ? item.ref : item.company.ref) + '" target="_blank">' + dict[item.from] + '</a></td>\n' +
                            '        </tr>\n' +
                            '        <tr>\n' +
                            '            <td colspan = "9">\n' +
                            '                <div style = "margin: 10px;">\n' +
                            '                    <code>\n' + item.content +
                            '                    </code>\n' +
                            '                </div>\n' +
                            '            </td>\n' +
                            '        </tr>'
                    }

                    $('.table-striped tbody').html(table_doc);
                    $('.posts_container').removeClass('hidden');
                    $('#matched_count').text('匹配到' + resp.data.length + '份招聘');
                    $(that).text('开始匹配');
                }
            }
        });
    });

    $('#clean').click(function () {
        $('#job_name').val('');
        $('#salary').val('');
        $('#city').val('');
        $('#work_time').val('');
        $('#education').val('');
        $('#introduce').val('');

        $('.posts_container').addClass('hidden');
    });

    $.ajax({
        url: '/posts_total',
        dataType: 'json',
        success: function (resp) {
            if (resp.success) {
                $('#topInfo span span').text(resp.total);
            }
        }
    });
    // setInterval(function () {
    //
    // }, 3000);

    // 可查询的岗位
    $.ajax({
        url: '/jobs_available',
        dataType: 'json',
        success: function (resp) {
            if (resp.success) {

                var html = "";

                for (var i = 0; i < resp.data.length; i++) {
                    html += '<span class = "label label-default" style="margin-left: 5px;float:left;margin-bottom: 5px;padding:6px;">' + resp.data[i] + '</span>';
                }

                $('#jobs_available').html(html);
                $('#jobs_available span').click(function () {
                    $('#job_name').val($(this).text());
                })
            }
        }
    });
});